package com.zuxia.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class TimeUtils {
    //定义常量
    public final static long DAY_MILLI_SECOND = 24*60*60*1000L; //每一天的毫秒数
    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();

        // 定义日期时间的格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        // 使用定义的格式格式化当前日期时间
        String formattedDateTime = now.format(formatter);
        System.out.println(formattedDateTime);
    }
    public static String getY_M_D_H_M_S(Long m){
        return getStampDate(m,"yyyy-MM-dd HH:mm:ss");
    }

    public static Long getCurrentTime() {
        LocalDate today = LocalDate.now();

        ZonedDateTime zdt = today.atStartOfDay(ZoneId.systemDefault());

        //年月日
        long timestamp = zdt.toInstant().toEpochMilli();

        System.out.println(getStampDate(timestamp,"yyyy-MM-dd"));
        return timestamp;
    }

    public static String getStampDate(Long timeStamp,String patter){
        SimpleDateFormat sdf = new SimpleDateFormat(patter);
        return sdf.format(new Date(timeStamp));
    }

    public static Long getDateStamp(String dateStr,String patter){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        Date parse = null;
        try {
            parse = sdf.parse(dateStr);
        } catch (ParseException e) {
            throw new RuntimeException(e); //日期转换异常 一般就是格式没对上  【参数验证时需要规避】
        }

        return parse.getTime();
    }

}
